package com.tianqu.tms.www.car.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.www.car.action.CarInnerListTopAction;
import com.tianqu.tms.www.car.actionform.CarSeniorSearchInputActionForm;

/**
 * @author zhangxueliang
 * 高级搜索 - 进出查询结果controller
 */
public class CarSearchInoutResultController extends TmsController{
	private Logger logger = Logger
	.getLogger(CarSearchInoutResultController.class);
	public static final String PAGING = "paging";
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controllor = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controllor.begin();
			// 从前台传过来一个form表单
			CarSeniorSearchInputActionForm seriorForm = (CarSeniorSearchInputActionForm) uc
					.getInputForm();

			Car car = Car.getInstance();

			if (uc.getAttribute(CarInnerListTopAction.PAGENO) != null) {
				car.setPageNo(Integer.parseInt((String) uc
						.getAttribute(CarInnerListTopAction.PAGENO)));
			}
			if (uc.getAttribute(CarInnerListTopAction.MAXINDEX) != null) {
				car.setMaxIndex(Integer.parseInt((String) uc
						.getAttribute(CarInnerListTopAction.MAXINDEX)));
			}
			if (uc.getAttribute(CarInnerListTopAction.SORTKEY) != null) {
				car.setSortKey(Integer.parseInt((String) uc
						.getAttribute(CarInnerListTopAction.SORTKEY)));
			}
			if (uc.getAttribute(CarInnerListTopAction.ASC) != null) {
				car.setAsc((new Boolean((String) uc
						.getAttribute(CarInnerListTopAction.ASC)))
						.booleanValue());
			}
			List list = new ArrayList();
			if (uc.getAttribute("seniorFrom") != null) {
				// 判定翻页
				seriorForm = (CarSeniorSearchInputActionForm) uc.getAttribute("seniorFrom");										
			}
			list = car.findInoutResult(seriorForm.getSerial(), seriorForm.getThreadStatus(),
					seriorForm.getGateId(), seriorForm.getStartTime(),
					seriorForm.getEndTime());
			uc.setAttribute(TmsConstants.VIEWHELP, list);
			uc.setAttribute(PAGING, car);
			// uc.removeAttribute("seniorFrom");
			uc.setAttribute("seniorFrom", seriorForm);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controllor.commit();
		} catch (Exception e) {
			try {
				controllor.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		
	}

}
